clear all
global system "linux"

if "${system}" == "linux" {
	global code "/"
	global s "/"
}

run "${code}${s}_set-path.do"


args numindex

cap log close _all
log using "${log}/log-crism-refi-`numindex'", name("log`numindex'") text replace

local MaxDepVar = 7
local MaxSpec = 3
local MaxIf = 2

local TotalNum = `MaxDepVar'* `MaxSpec'* `MaxIf' 


if `numindex' <= `TotalNum' { // All Sample
	local DepVarType = ceil(`numindex'/ `MaxSpec'/ `MaxIf')
	
	local numindex2 = `numindex' - (`DepVarType' - 1) * `MaxSpec'* `MaxIf'
	local Spec = ceil(`numindex2'/ `MaxIf')

	local numindex3 = `numindex2' - (`Spec'-1)* `MaxIf'
	local If = `numindex3'
}
if `numindex' > `TotalNum' { // Those who plain-refi'ed
	local MaxDepVar = 4
	local MaxIf = 2
	
	local DepVarType = ceil((`numindex' - `TotalNum')/ `MaxIf')
	local Spec = 4
	
	local numindex3 = (`numindex' - `TotalNum') - (`DepVarType'-1)* `MaxIf'
	local If = `numindex3' 
}	

if `DepVarType' == 1 global DepVar "auto_orig"
if `DepVarType' == 2 global DepVar "auto_orig_amount"
if `DepVarType' == 3 global DepVar "auto_orig3"
if `DepVarType' == 4 global DepVar "auto_orig3_amt"
if `DepVarType' == 5 global DepVar "plainrefi5pct1"
if `DepVarType' == 6 global DepVar "cashrefi5pct1"	
if `DepVarType' == 7 global DepVar "nonrefi"	

global If_common "primary_fg==1 &  prop_zip_code == zip_code & owneroccupied==1 & orig_qtr >= yq(2006,1)"

if `If' == 1 global If "${If_common}"
if `If' == 2 global If "inrange(diff, -25, 25) & ${If_common}" 
		


*** Load data
use "${LPS}/reg-data-loanlevel.dta", clear // created in "tba_elig/_crism-reg-data.do"

keep if data_as_of <= ym(2012, 12)
gen orig_ym = mofd(orig_date)
format orig_ym %tm

gen nonrefi = prepaid - anyrefi


*** regression
global controls "c.loanage##c.loanage##c.loanage i.purpose_id i.investor_id fctr orig_amount i.risk_bin FullDoc i.ultv_bin i.diff_rate"
global controls "$controls loan_to_value owneroccupied pp_pen pp_pen_period past_delinq* past_forcl io_loan io_period"
global auto_controls "lag_auto_balance* c.auto_orig_after##c.auto_orig_after"

global poly1 "diff 1.lowbal#c.diff"
global poly2 "$poly1 c.diff#c.diff 1.lowbal#c.diff#c.diff"
global poly3 "$poly2 c.diff#c.diff#c.diff 1.lowbal#c.diff#c.diff#c.diff"


if `Spec' <= 3 { // including both those who refi'ed and those who did not
	keep if ${If}
	
	reghdfe $DepVar lowbal ${poly`Spec'} $controls $auto_controls, a(FEzip=i.zip_ym FEorig=i.orig_ym) vce(robust)
	estimate save "${Estimates}/crism-$DepVar-spec`Spec'-If`If'-upto2012", replace
	
	
	if `Spec' == 3 {
		append using "${temp}/crism-simul-sample.dta"
	
		cap drop resid
		predict resid, r
		replace resid = 0 if simul == 1
		
		foreach x in lowbal $poly3 {
			replace resid = resid + _b[`x']* (`x')
		}
		
		su $DepVar 
		replace resid = resid + `r(mean)'
		
		replace diff =  ceil(diff/2.5)*2.5 if simul~=1
		keep if resid ~= . 
		
		
		preserve
			collapse resid, by(diff simul) fast
			
			twoway scatter resid diff if simul~=1, xline(0)   ///
			 xtitle("Remaining Balance - National CLL ($1000)", size(medlarge)) ytitle("") ylabel(, angle(0)) ///
			 xlabel(-50 -25 0 25 50)
			 
			graph export "${figure}/rdplot-crism-$DepVar-If`If'-upto2012.pdf", replace		
	
		restore
	}
}

if `Spec' == 4 { // only for those who refi'ed
	keep if refitime~=.
	keep if EverPlainRefi == 1
	keep if ${If}
	keep if inrange(refitime, 1, 11)
	reghdfe $DepVar ib6.refitime $controls $auto_controls, a(zip_ym orig_qtr) cluster(zip_ym)
	estimate save "${Estimates}/crism-$DepVar-spec`Spec'-If`If'-upto2012", replace
}

cap log close _all
